package io.kerns.core.web.response;

import org.apache.commons.lang3.StringUtils;
import io.kerns.core.common.CodeMsg;
/**
 * 返回结果的辅助类
 */
public class ResponseUtils {

    public static Response success(Object data) {
        Response result = new Response();
        result.setSuccess(true);
        result.setCode("200");
        result.setData(data);
        return result;
    }

    public static boolean isSuccess(Response response) {
        return StringUtils.equals(response.getCode(), "200") && response.isSuccess();
    }

    public static Response success() {
        Response result = new Response();
        result.setSuccess(true);
        result.setCode("200");
        return result;
    }

    public static Response fail(String errorCode, String message, Integer subCode) {
        Response result = new Response();
        result.setSuccess(false);
        result.setCode(errorCode);
        return result;
    }

    public static Response fail(CodeMsg code) {
        Response result = new Response();
        result.setSuccess(false);
        result.setCode(code.getCode());
        result.setMessage(code.getMsg());
        return result;
    }

    public static Response fail(String msg) {
        Response result = new Response();
        result.setSuccess(false);
        result.setCode("999");
        result.setMessage(msg);
        return result;
    }

    public static Response fail(String errorCode, String message) {
        Response result = new Response();
        result.setSuccess(false);
        result.setCode(errorCode);
        result.setMessage(message);
        return result;
    }


}
